package com.gjy.mongod.util;

import com.gjy.mongod.codec.DateDomainProvider;
import com.gjy.mongod.codec.SpringProvider;
import com.gjy.mongod.domain.DateDomain;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-15 11:05:20
 */
public class MongoUtil {

    public static <T> MongoCollection<T> mongoClass(String db, String col, Class<T> clazz) {
        CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
//                CodecRegistries.fromProviders(new GoodsProvider()),
//                CodecRegistries.fromProviders(new GenericProvider()),
                CodecRegistries.fromProviders(new SpringProvider()),
                CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry())
        );
        MongoClientSettings build = MongoClientSettings.builder()
                .codecRegistry(codecRegistry)
                .applyConnectionString(new ConnectionString("mongodb://192.168.200.181:27017"))
                .build();

        return MongoClients.create(build)
                .getDatabase(db)
                .getCollection(col, clazz);
        // MongoClient client = MongoClients.create("mongodb://192.168.200.181:27017");
        // return client.getDatabase(db).getCollection(col, clazz);
    }

    public static MongoCollection<Document> mongo(String db, String col) {
        MongoClient client = MongoClients.create("mongodb://192.168.200.181:27017");
        MongoDatabase database = client.getDatabase(db);
        return database.getCollection(col);
    }

    public static GridFSBucket gridFs(String db) {
        MongoClient client = MongoClients.create("mongodb://192.168.200.181:27017");
        MongoDatabase database = client.getDatabase(db);
        return GridFSBuckets.create(database);
    }

    public static MongoCollection<DateDomain> mongo1(String db, String col) {
        CodecRegistry registries = CodecRegistries.fromRegistries(
                CodecRegistries.fromProviders(new DateDomainProvider()),
                CodecRegistries.fromProviders(MongoClientSettings.getDefaultCodecRegistry())
        );
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .codecRegistry(registries)
                .applyConnectionString(new ConnectionString("mongodb://192.168.200.181:27017"))
                .build();

        MongoClient client = MongoClients.create(mongoClientSettings);
        MongoDatabase database = client.getDatabase(db);
        return database.getCollection(col, DateDomain.class);
    }

    public static MongoCollection<DateDomain> mongoSec(String db, String col) {
        CodecRegistry registries = CodecRegistries.fromRegistries(
                CodecRegistries.fromProviders(new DateDomainProvider()),
                CodecRegistries.fromProviders(MongoClientSettings.getDefaultCodecRegistry())
        );
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .codecRegistry(registries)
                .credential(MongoCredential
                        .createCredential("myUserAdmin", "admin", "123456".toCharArray()))
                .applyConnectionString(new ConnectionString("mongodb://192.168.200.181:28017"))
                .build();

        MongoClient client = MongoClients.create(mongoClientSettings);
        MongoDatabase database = client.getDatabase(db);
        return database.getCollection(col, DateDomain.class);
    }

    public static MongoCollection<Document> mongos(String db, String col) {
        return MongoClients.create("mongodb://192.168.200.181:27018")
                .getDatabase(db)
                .getCollection(col);
    }
}
